package DAL;

import DatabaseAccess.SQLHelper;
import java.util.List;
import Entities.TblBranch;
import java.sql.ResultSet;
import java.util.ArrayList;

public class BranchDAL {

    private ResultSet objResult;
    public List<TblBranch> list;

    public BranchDAL() {
    }

    public boolean add(TblBranch branch) {
        int result = 0;
        String[] params = new String[5];
        params[0] = branch.getBranchName();
        params[1] = branch.getAddress();
        params[2] = branch.getEmail();
        params[3] = branch.getPhoneNo();
        params[4] = branch.getFax();

        try {
            result = SQLHelper.executeUpdate("procInsertBranch", params);
        } catch (Exception ex) {
            System.out.println("Error: " + ex.getMessage());
        }

        return result == 1 ? true : false;
    }

    public boolean update(TblBranch branch) {
        int result = 0;
        String[] params = new String[6];
        params[0] = String.valueOf(branch.getBranchId());
        params[1] = branch.getBranchName();
        params[2] = branch.getAddress();
        params[3] = branch.getEmail();
        params[4] = branch.getPhoneNo();
        params[5] = branch.getFax();

        try {
            result = SQLHelper.executeUpdate("procUpdateBranch", params);
        } catch (Exception ex) {
            System.out.println("Error: " + ex.getMessage());
        }

        return result == 1 ? true : false;
    }

    public boolean delete(TblBranch branch) {
        int result = 0;
        String[] params = new String[1];
        params[0] = String.valueOf(branch.getBranchId());

        try {
            result = SQLHelper.executeUpdate("procDeleteBranch", params);
        } catch (Exception ex) {
            System.out.println("Error: " + ex.getMessage());
        }

        return result == 1 ? true : false;
    }

    public List<TblBranch> getAll() {
        list = new ArrayList<TblBranch>();
        try {
            objResult = SQLHelper.executeQuery("procGetAllBranch", null);
            while (objResult.next()) {
                TblBranch branch = new TblBranch();
                branch.setBranchId(objResult.getInt("BranchId"));
                branch.setBranchName(objResult.getString("BranchName"));
                branch.setAddress(objResult.getString("Address"));
                branch.setEmail(objResult.getString("Email"));
                branch.setPhoneNo(objResult.getString("PhoneNo"));
                branch.setFax(objResult.getString("Fax"));

                list.add(branch);
            }

            return list;
        } catch (Exception ex) {
            System.out.println("Error: " + ex.getMessage());
            return null;
        }
    }

    public TblBranch getById(int id) {
        TblBranch branch = new TblBranch();
        String[] params = new String[1];
        params[0] = String.valueOf(id);
        try {
            objResult = SQLHelper.executeQuery("procGetBranchById", params);
            if (objResult.next()) {
                branch.setBranchId(objResult.getInt("BranchId"));
                branch.setBranchName(objResult.getString("BranchName"));
                branch.setAddress(objResult.getString("Address"));
                branch.setEmail(objResult.getString("Email"));
                branch.setPhoneNo(objResult.getString("PhoneNo"));
                branch.setFax(objResult.getString("Fax"));
            }

            return branch;
        } catch (Exception ex) {
            System.out.println("Error: " + ex.getMessage());
            return null;
        }
    }
}
